M = {}

local newPlayer = function(x, y,planetGroup)
    local player = display.newCircle(x, y, 10)
    player:setFillColor(255, 0, 0)
    player.planeta = nil
    physics.addBody(player, "dynamic", { density = 0.1, friction = 0.2, bounce = 0.0, radius = 10 })
    player.cx=player.x
    player.cy=player.y
    
    player.playerSprite = display.newImage( "kiwi.png" )
    
    local colision = 0
    
    local function updatePlayer()
        local gravity_constant = 9000
        
        if (player.planeta == nil) then
            for i = 1, planetGroup.numChildren do
                
                local vp = vector2d:new(player.x, player.y)
                local vi = vector2d:new(planetGroup[i].x, planetGroup[i].y)
                vi:sub(vp)
                local magnitud = vi:magnitude()
                if(magnitud < planetGroup[i].radio + planetGroup[i].atmosfera + 10)then
                    vi:normalize()
                local mag2 = magnitud * magnitud
                local fuerza = gravity_constant / mag2
                vi:mult(fuerza)
                player:applyForce(vi.x, vi.y, player.x, player.y)
                    end
                
               
            end
            --print("vuela")
        else
            local vp = vector2d:new(player.x, player.y)
            local vi = vector2d:new(player.planeta.x, player.planeta.y)
            vi:sub(vp)
            local magnitud = vi:magnitude()
            vi:normalize()
            local mag2 = magnitud * magnitud
            local fuerza = gravity_constant / mag2
            vi:mult(fuerza)
            player:applyForce(vi.x, vi.y, player.x, player.y)
            --print("planeta")
        end
        
        local vp = vector2d:new(player:getLinearVelocity())
        if ( vp:magnitude() < 180) then
            if(player.planeta ~= nil)then
            vp:normalize()
            vp:mult(0.15)
            player:applyForce(vp.x, vp.y, player.x, player.y)
            end
        else
            if(vp:magnitude() > 450)then
                vp:normalize()
            vp:mult(450)
            player:setLinearVelocity( vp.x, vp.y )
                
                end
            
            end
    end
    
    
    local onUpdate = function(event)
        updatePlayer()
    end
    Runtime:addEventListener("enterFrame", onUpdate)
    
    --impulso
    local function impulsar(event)
        if (event.phase == "began") then
            if (player.planeta ~= nil) then
                
                local vp = vector2d:new(player.x, player.y)
                local vi = vector2d:new(player.planeta.x, player.planeta.y)
                vp:sub(vi)
                vp:normalize()
                player:applyLinearImpulse(vp.x / 1.5, vp.y / 1.5, player.x, player.y)
                player.planeta = nil
                colision = 10
            end
        end
    end
    
    Runtime:addEventListener("touch", impulsar)
    
    
    local function onPlayerCollision(self, event)
        if (event.phase == "began") then
            if(colision == 0)then
                player.planeta = event.other
                player.cx=player.x
                player.cy=player.y
            end        
        elseif (event.phase == "ended") then
        end
    end
    
    player.collision = onPlayerCollision
    player:addEventListener("collision", player)
    
    local function angleBetween ( srcObj, dstObj )
        
        local xDist = dstObj.x-srcObj.x ; local yDist = dstObj.y-srcObj.y
        local angleBetween = math.deg( math.atan( yDist/xDist ) )
        if ( srcObj.x < dstObj.x ) then angleBetween = angleBetween+90 else angleBetween = angleBetween-90 end
        return angleBetween
    end
    
    local function updatePlayer()
        player.playerSprite.x = player.x
        player.playerSprite.y = player.y
        if(player.planeta~= nil )then
            player.playerSprite.rotation = angleBetween ( player.planeta, player )
        end
        if(colision>0)then
            colision = colision -1
        end
    end
    
    
    Runtime:addEventListener("enterFrame", updatePlayer)
    
    return player
end

M.newPlayer = newPlayer

return M